﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Medianamik.Client.DAL;
using Medianamik.Client.MVP.Model.DataAccess;

namespace Medianamik.Client.MVP.Model.Service
{
    public class NodeUtils : INodeUtils
    {
        private readonly IMedianamikRepository<IBaseType> _repository;

        public NodeUtils(IMedianamikRepository<IBaseType> repository)
        {
            _repository = repository;
        }

        public bool IsParent(Guid parentId, Guid nodeId)
        {
            return IsParent(parentId, nodeId, false);
        }
        public bool IsParent(Guid parentId, Guid nodeId, bool inclusive)
        {
            return IsParent(parentId, _repository.Get(nodeId), inclusive);
        }
        
        public bool IsParent(Guid parentId, IBaseType node)
        {
            return IsParent(parentId, node);
        }
        public bool IsParent(Guid parentId, IBaseType node, bool inclusive)
        {
            //le inclusive sert a rien je crois.
            if (inclusive && parentId == node.NodeId)
                return true;
            if (node == null || node.Path == null)
                return false;

            return node.Path.Contains(parentId.ToString().ToUpper());
        }

        public bool IsParent(IBaseType parentNode, IBaseType node)
        {
            return IsParent(parentNode.NodeId, node);
        }

        public bool IsParent(IBaseType parentNode, IBaseType node, bool inclusive)
        {
            return IsParent(parentNode.NodeId, node, inclusive);
        }

        public bool IsParent(IBaseType parentNode, Guid nodeId)
        {
            return IsParent(parentNode.NodeId, nodeId);
        }

        public bool IsParent(IBaseType parentNode, Guid nodeId, bool inclusive)
        {
            return IsParent(parentNode.NodeId, nodeId, inclusive);
        }
    }
    public interface INodeUtils
    {
        bool IsParent(Guid parentId, Guid nodeId);
        bool IsParent(Guid parentId, Guid nodeId, bool inclusive);

        bool IsParent(Guid parentId, IBaseType node);
        bool IsParent(Guid parentId, IBaseType node, bool inclusive);

        bool IsParent(IBaseType parentNode, IBaseType node);
        bool IsParent(IBaseType parentNode, IBaseType node, bool inclusive);

        bool IsParent(IBaseType parentNode, Guid nodeId);
        bool IsParent(IBaseType parentNode, Guid nodeId, bool inclusive);
    }
}
